<?php
/* @var $this DefaultController */
/* @var $model Doitac */

$this->breadcrumbs=array(
    'Doitacs'=>array('index'),
);
?>
<link href="<?php echo Yii::app()->theme->baseUrl ?>/css/jquery-ui.css" type="text/css" rel="stylesheet">
<script src="<?php echo Yii::app()->theme->baseUrl ?>/js/autoNumeric.js"></script>
<script src="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/bootstrap-tokenfield.js"></script>
   <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/tokenfield-typeahead.css" type="text/css" rel="stylesheet">
    <!-- Tokenfield CSS -->
    <link href="<?php echo Yii::app()->theme->baseUrl ?>/js/bootstrap-tokenfield/css/bootstrap-tokenfield.css" type="text/css" rel="stylesheet">
    <link href="<?php echo Yii::app()->theme->baseUrl; ?>/css/jquery-ui.css" rel="stylesheet" type="text/css" />
<div class="col-md-14">
    <div class="box box-primary">
        <div class="box-header">
            <h3 class="box-title">Nhập hóa đơn</h3>
        </div><!-- /.box-header -->
        <div class="box-body">
            <dl class="dl-horizontal">
                <dt>Lựa chọn đối tác:</dt>
                <?php $this->widget('DoiTac.components.AutocompleteWidget',array('id'=>'doitac_input','action'=>Yii::app()->createUrl("/DoiTac/default/load")));  ?>
                <dt>Lựa chọn thành viên:</dt>
                <?php $this->widget('DoiTac.components.AutocompleteWidget',array('id'=>'khachhang_input','action'=>Yii::app()->createUrl("/KhachHang/default/load")));  ?>
                <dt>Số phiếu thanh toán:</dt>
                <dd><input type="text" id="phieu_thanh_toan" class="form-control input_text"></dd>
                <dt>Số tiền:</dt>
                <dd><input type="text" id="tien" class="form-control input_text"></dd>
                <input type="hidden" value="0" id="id_hoadon">
                <dt>Ngày sử dụng:</dt>
                <dd><input type="text" id="date" class="form-control input_text"></dd>
                <script type="text/javascript">
                        $('#date').datepicker();
                </script>
                <dd>
                    <p id="info"></p>
                    <a id="save_hoadon" class="btn btn-primary">Tạo hóa đơn</a>
                </dd><br>
                <div class="pull-left">
                    <input type="text" id="date1">
                    đến
                    <input type="text" id="date2">
                    <button id="loc">Lọc</button>
                </div>
            </dl>
         <div class="pull-right" style="margin-bottom:10px;">
                <?php echo CHtml::image("images/trangthai-0.png", "DORE",array("class"=>"image_view")); ?><label>Chưa thanh toán</label>
                <?php echo CHtml::image("images/trangthai-1.png", "DORE",array("class"=>"image_view")); ?><label>Thanh toán một phần</label>
                <?php echo CHtml::image("images/trangthai-2.png", "DORE",array("class"=>"image_view")); ?><label>Đã thanh toán</label>
            </div>
        </div><!-- /.box-body -->
        <?php $this->widget('ext.GLGridView', array(
            'id'=>'doitac-grid1',
            'cssFile' => Yii::app()->theme->baseUrl . '/css/gridView.css',
            'dataProvider'=>$data,
            'columns'=>array(
                array(
                    'name'=>'id',
                    'type'=> 'raw',
                    'value'=> 'CHtml::hiddenField("id", $data->id,array("class"=>"idhoadon")).$data->id', 
                ),
                array(
                    'name'=>'id_khachhang',
                    'htmlOptions'=>array('class'=>'name-ten'),
                    'value'=> '$data->khachhang == null ? "Không có" : $data->khachhang->ten',
                ),
                array(
                    'name'=>'id_doitac',
                    'htmlOptions'=>array('class'=>'name-ten'),
                    'value'=> '$data->doitac == null ? "Không có" : $data->doitac->ten',
                ),
                array(
                        'name'=>'ms_khachhang',
                        'htmlOptions'=>array('class'=>'ma-khachhang'),
                ),
                array(
                        'name'=>'ma_so_doi_tac',
                        'value'=>'$data->doitac == null ? "Không có" : $data->doitac->ma_so',
                        'htmlOptions'=>array('class'=>'ma-doitac'),
                    ),
                'so_phieu_thanh_toan',
                array(
                    'name'=>'so_tien',
                    'value'=>'getMoney($data->so_tien)'
                ),
                array(
                    'name'=>'chiet_khau',
                    'value'=>'getMoney($data->chiet_khau)'
                ),
                'ngay_su_dung',
                array(
                    'name'=>'Thanh toán với đối tác',
                    'type'=>'raw',
                    'value'=> 'CHtml::image("images/trangthai-{$data->trangthai_thanhtoan_dt}.png", "DORE",array("class"=>"image"));', 
                ),
                array(
                    'name'=>'Thanh toán với khách hàng',
                    'type'=>'raw',
                    'value'=> 'CHtml::image("images/trangthai-{$data->trangthai_thanhtoan_kh}.png", "DORE",array("class"=>"image"));', 
                ),
            ),
        )); ?>
    </div><!-- /.box -->
</div>
<script type="text/javascript">
    $('#tien').autoNumeric('init');
    $( "#date1" ).datepicker();
    $( "#date2" ).datepicker();
    $('body').on('click', '#loc', function(event) {
        if ($('#date1').val() != '' && $('#date2').val() != '') {
            var date1 = $('#date1').val();
            var date2 = $('#date2').val();
            $.fn.yiiGridView.update('doitac-grid1',{data: {date1:date1,date2:date2}});
        };
    }); 
</script>
<style type="text/css">
    .input_text{
        width: 30%;
        margin-bottom: 10px;
    }
    .tokenfield{
        margin-bottom: 10px;   
        width: 30%;
    }
    .error{
        border-color: #f56954 !important;
        box-shadow: none;
    }
    .ui-autocomplete{
        width: 19% !important;
        min-width: 12% !important;
    }
</style>
<input type="hidden" value="0" id="error">
<script type="text/javascript">
    $('input[type=text]').focus(function () {
       $(this).removeClass('error');    
       $(this).parent().removeClass('error');    
    });
    $('#save_hoadon').click(function () {
         var check = 0;
       $.each($('.input_text'),function() {
           var t = $(this);
           if (t.val() =="") {
                if (t.attr('type') == "hidden") {
                    t.parent().find('.form-control').addClass('error');
                }else{
                    t.addClass('error');
                }
                check++;
           };
       })
       var phieu = $('#phieu_thanh_toan').val();
        if (phieu != '') {
            $.ajax({
                url: "<?php echo Yii::app()->createUrl('/DoiTac/doitacKhachhang/kiemtrasophieu'); ?>",
                type: 'POST',
                data: {phieu:phieu},
                success: function (data) {
                    if (data == 1) {
                        check++;
                        console.log(check);
                        $('#phieu_thanh_toan').addClass('error');
                        $('#error').val("1");
                    }else{
                        console.log(check);
                       if (check == 0 ) {
                            var idDoitac = $('.doitac_input .input_text').val();
                            var idKhachhang = $('.khachhang_input .input_text').val();
                            var tien = $('#tien').val().replace(',','');
                            while(tien.split(',').length != 1){
                                console.log(tien.split(',').length);
                                tien = tien.replace(',','');
                            }
                            var idhoadon = $('#id_hoadon').val();
                            $.ajax({
                                url: "<?php echo Yii::app()->createUrl('/DoiTac/doitacKhachhang/create'); ?>",
                                type: 'POST',
                                dataTyle:"json",
                                data: {idKhachhang:idKhachhang,idDoitac:idDoitac,phieu:phieu,tien:tien,id:idhoadon,date:$('#date').val()},
                                success: function (data) {
                                    $('#info').html(data.info);
                                    if (data.status) {
                                        $('#phieu_thanh_toan').val("");
                                        $('#tien').val("");
                                        $('#khachhang_input').tokenfield('setTokens', '{}');
                                        $('#date').val("");
                                        $('#error').val("0");
                                        $("#id_hoadon").val("");
                                        $.fn.yiiGridView.update('doitac-grid1',{data: {id:idDoitac}});
                                    };
                                    setTimeout(function() {
                                        $('#info').html('');
                                    }, 2000);
                                }
                            });
                       };
                    }
                }
            });
        };
    });
</script>
